package com.plian.system.controller.sys.orgzation;

import com.plian.Tools.TokenUtil;
import com.plian.system.annotation.LogOperation;
import com.plian.system.entity.sys.ServiceOrg;
import com.plian.system.model.ListResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.sys.orgzation.IServiceOrgService;
import com.plian.system.vo.sys.BaseServiceOrgRecordVO;
import com.plian.system.vo.sys.ServiceOrgVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping(value = "api/serviceorg")
@Api(value = "业务组织", tags = "业务组织接口")
public class ServiceOrgController {

    private IServiceOrgService serviceOrgService;

    @GetMapping("/{baseServiceOrgId}/latest/tree")
    @ApiOperation(value = "获取最新的树", notes = "传入baseServiceOrgId")
    public R<List<ServiceOrgVO>> latestTree(@PathVariable Long baseServiceOrgId) {
        return R.data(serviceOrgService.latestTree(baseServiceOrgId));
    }

    @PostMapping("/submit")
    @ApiOperation(value = "更新或修改", notes = "传入serviceOrg")
    @LogOperation("业务组织-提交")
    public R submit(@RequestBody ServiceOrg serviceOrg) {
        return R.status(serviceOrgService.submit(serviceOrg));
    }

    @PostMapping("/remove")
    @ApiOperation(value = "删除 单个", notes = "传入serviceOrg")
    @LogOperation("业务组织-删除")
    public R latestTree(@RequestBody ServiceOrg serviceOrg) {
        return R.status(serviceOrgService.remove(serviceOrg));
    }

    @PostMapping("/{baseServiceOrgId}/record-list")
    @ApiOperation(value = "获取修改记录列表", notes = "传入baseServiceOrgId、query")
    public ListResult<BaseServiceOrgRecordVO> recordList(@PathVariable Long baseServiceOrgId, @RequestBody Query query) {
        return serviceOrgService.recordList(baseServiceOrgId, query);
    }

    @PostMapping("/{baseServiceOrgId}/change-effective-time")
    @ApiOperation(value = "修改生效时间", notes = "传入baseServiceOrgId")
    @LogOperation("业务组织-修改生效时间")
    public R changeEffectiveTime(@PathVariable Long baseServiceOrgId, @RequestBody BaseServiceOrgRecordVO baseServiceOrgRecordVO) {
        return R.status(serviceOrgService.changeEffectiveTime(baseServiceOrgId, baseServiceOrgRecordVO));
    }

//    @GetMapping("/{formType}/time-tree") TODO DEL
//    @ApiOperation(value = "根据 formType、时间获取树", notes = "传入formType、queryTime不传默认当前时间")
//    public R<List<ServiceOrgVO>> formTypeTimeTree(@PathVariable Integer formType, @RequestParam("queryTime") String queryTime) {
//        return R.data(serviceOrgService.getTreeByFormTypeAndTime(formType, queryTime));
//    }

    @GetMapping("/{formType}/token-time-tree")
    @ApiOperation(value = "根据 登录信息 formType、时间获取树", notes = "传入formType、queryTime不传默认当前时间")
    public R<List<ServiceOrgVO>> formTypeTokenTimeTree(@PathVariable Integer formType,
                                      @RequestParam("queryTime") String queryTime) {
        return R.data(serviceOrgService.getTreeByFormTypeAndTime(TokenUtil.getTokenOrgId(), formType, queryTime));
    }
}
